//package com.net.xpay.openapi.controller.openapi.paytrans;
//
//import com.google.common.base.Strings;
//import com.net.xpay.openapi.aop.OpenApi;
//import com.net.xpay.openapi.dto.OpenAlipayAuthAlipayUserUnsignDto;
//import com.net.xpay.openapi.dto.OpenApiAlipayAuthAuthPageGoDto;
//import com.net.xpay.openapi.dto.OpenApiAlipayAuthAuthResultGetDto;
//import com.net.xpay.openapi.model.OpenAlipayAuthAlipayUserUnsignModel;
//import com.net.xpay.openapi.model.OpenApiAlipayAuthAuthPageGoModel;
//import com.net.xpay.openapi.model.OpenApiAlipayAuthAuthResultGetModel;
//import com.net.xpay.openapi.protocol.OpenApiProtocol;
//import com.net.xpay.openapi.protocol.OpenApiResult;
//import com.net.xpay.openapi.service.OpenApiAlipayAuthService;
//import com.net.common.exception.BusinessException;
//import com.net.common.util.SerializationUtil;
//import com.net.xpay.alipay.PoseidonAlipayClient;
//import com.net.xpay.common.annotation.ApiMonitoring;
//import com.net.xpay.common.domain.User;
//import com.net.xpay.common.domain.openapi.OpenApp;
//import com.net.xpay.common.helper.RedisHelper;
//import com.net.xpay.common.manager.UserManager;
//import com.net.xpay.common.manager.UserShardManager;
//import com.net.xpay.common.manager.openapi.OpenAppManager;
//import com.net.xpay.core.constant.PoseidonErrorCode;
//import com.net.xpay.core.domain.AlipayUser;
//import com.net.xpay.core.enums.AlipayUserStatus;
//import com.net.xpay.core.manager.AlipayUserManager;
//import io.swagger.annotations.Api;
//import io.swagger.annotations.ApiOperation;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.web.bind.annotation.*;
//
///**
// * 2020/6/28 09:46
//
// */
//@RestController
//@Slf4j
//@RequestMapping("/apiv1/openapi/alipay-auth")
//@Api(tags = "第三方开放调用/批量有密签约接口", description = "第三方开放调用 批量有密签约接口 net")
//public class OpenApiAlipayAuthController {
//
//    @Autowired
//    private UserManager userManager;
//
//    @Autowired
//    private UserShardManager userShardManager;
//
//    @Autowired
//    private AlipayUserManager alipayUserManager;
//
//    @Autowired
//    private PoseidonAlipayClient xpayAlipayClient;
//
//    @Autowired
//    private RedisHelper redisHelper;
//
//    @Autowired
//    private OpenAppManager openAppManager;
//
//    @Autowired
//    private OpenApiAlipayAuthService openApiAlipayAuthService;
//
//    /**
//     * 获取支付宝授权页面, 可进行签约
//     */
//    @OpenApi
//    @ApiMonitoring(value = "批量有密/签约支付宝")
//    @ApiOperation(value = "跳转支付宝签约页面")
//    @PostMapping("/go-auth-page")
//    @ResponseBody
//    public OpenApiResult<OpenApiAlipayAuthAuthPageGoDto> goAuthPage(@RequestBody OpenApiProtocol openApiProtocol) {
//        OpenApiAlipayAuthAuthPageGoModel authModel = SerializationUtil.str2Object(openApiProtocol.getBizContent(), OpenApiAlipayAuthAuthPageGoModel.class);
//        OpenApp openApp = openAppManager.getById(Long.valueOf(openApiProtocol.getAppId()));
//        User user = userManager.getById(openApp.getUserId());
//        String signUrl = openApiAlipayAuthService.doAlipayAuth(authModel, user);
//        OpenApiAlipayAuthAuthPageGoDto dto = new OpenApiAlipayAuthAuthPageGoDto();
//        dto.setSignUrl(signUrl);
//        return new OpenApiResult<>(dto);
//    }
//
//    /**
//     * 批量有密解约
//     */
//    @OpenApi
//    @ApiMonitoring(value = "批量有密/解约支付宝")
//    @ApiOperation(value = "批量有密解约")
//    @PostMapping("/un-sign")
//    @ResponseBody
//    public OpenApiResult<OpenAlipayAuthAlipayUserUnsignDto> unSignAlipayUser(@RequestBody OpenApiProtocol openApiProtocol) {
//        OpenAlipayAuthAlipayUserUnsignModel unsignModel = SerializationUtil.str2Object(openApiProtocol.getBizContent(), OpenAlipayAuthAlipayUserUnsignModel.class);
//        OpenApp openApp = openAppManager.getById(Long.valueOf(openApiProtocol.getAppId()));
//        User user = userManager.getById(openApp.getUserId());
//
//        if (Strings.isNullOrEmpty(unsignModel.getAlipayNickName())) {
//            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "请输入支付宝账号");
//        }
//        if (!Strings.isNullOrEmpty(unsignModel.getAlipayNickName())) {
//            AlipayUser alipayUser = alipayUserManager.findByAlipayNickName(user.getId(), unsignModel.getAlipayNickName());
//            if (alipayUser == null) {
//                throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "支付宝账号不存在");
//            }
//            alipayUser.setStatus(AlipayUserStatus.NO);
//            alipayUserManager.updateByPrimaryKeyWithVersion(alipayUser);
//        }
//        OpenAlipayAuthAlipayUserUnsignDto dto = new OpenAlipayAuthAlipayUserUnsignDto();
//        dto.setSuccess(true);
//        return new OpenApiResult<>(dto);
//    }
//
//    /**
//     * 查询支付宝签约结果
//     */
//    @OpenApi
//    @ApiMonitoring(value = "批量有密/查询支付宝签约结果")
//    @ApiOperation("查询支付宝签约结果")
//    @PostMapping("/get-auth-result")
//    public OpenApiResult<OpenApiAlipayAuthAuthResultGetDto> getAlipayAuthResult(@RequestBody OpenApiProtocol openApiProtocol) {
//        OpenApiAlipayAuthAuthResultGetModel queryModel = SerializationUtil.str2Object(openApiProtocol.getBizContent(), OpenApiAlipayAuthAuthResultGetModel.class);
//        OpenApp openApp = openAppManager.getById(Long.valueOf(openApiProtocol.getAppId()));
//        User user = userManager.getById(openApp.getUserId());
//
//        if (Strings.isNullOrEmpty(queryModel.getAlipayNickName()) && Strings.isNullOrEmpty(queryModel.getSignBizNo())) {
//            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "请输入支付宝账号");
//        }
//        AlipayUser alipayUser = null;
//        if (!Strings.isNullOrEmpty(queryModel.getAlipayNickName())) {
//            alipayUser = alipayUserManager.findByAlipayNickName(user.getId(), queryModel.getAlipayNickName());
//        } else if (!Strings.isNullOrEmpty(queryModel.getSignBizNo())) {
//            alipayUser = alipayUserManager.findBySignBizNo(user.getId(), queryModel.getSignBizNo());
//        }
//
//        if (alipayUser == null || alipayUser.getIsDeleted() == 1) {
//            throw new BusinessException(PoseidonErrorCode.ALIPAY_USER_NO_EXIST, "该用户尚未发起签约或已被删除");
//        }
//
//        OpenApiAlipayAuthAuthResultGetDto dto = OpenApiAlipayAuthAuthResultGetDto.getInstance();
//        dto.setAlipayNickName(alipayUser.getAlipayNickName());
//        dto.setStatus(alipayUser.getStatus());
//        return new OpenApiResult<>(dto);
//    }
//
//}
